package com.oocl.service.impl;

import java.util.Comparator;

import com.oocl.action.impl.DataProcessAction;
import com.oocl.action.impl.FileProcessAction;
import com.oocl.comparator.CustomerComparatorByBirth;
import com.oocl.comparator.CustomerComparatorById;
import com.oocl.pojo.Customer;
import com.oocl.service.CommandService;

/**
 * 处理指令
 * @author GANAB
 *
 */
public class CommandServiceImpl implements CommandService {

	public CommandServiceImpl() {}
	
	public StringBuilder service(String params) throws Exception {
		if(params!=null){
			params = params.trim();
			String[] paramArray = params.split(" ");
			if(paramArray.length<3){
				return new StringBuilder("Parameter error! please try again!\r\n");
			}
			String command = paramArray[0];
			String format = paramArray[1];
			String sortby = paramArray[2];
			
			//判断Comparator的类型
			Comparator<Customer> comp = null;
			if("birth".equals(sortby)){
				comp = new CustomerComparatorByBirth();
			}else{
				comp = new CustomerComparatorById();
			}

			//判断指令
			if("L".equals(command)){
				return new DataProcessAction(comp, format).action();
			}else if("C".equals(command)){
				boolean result = new FileProcessAction(comp, format).action();
				if(result){
					return new StringBuilder("Create [dataPrint."+format+"] file success! \r\n");
				}else{
					return new StringBuilder("Create file failure! \r\n");
				}
			}else{
				return new StringBuilder("Not such command！\r\n");
			}

		}
		return new StringBuilder("Parameter error! please try again!\r\n");
	}

}
